package com.site.service.impl.dining;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.site.dao.dining.NewsAdvisoryDao;
import com.site.dao.dining.NewsSortDao;
import com.site.dto.dining.NewsAdvisoryBizBean;
import com.site.model.dining.NewsAdvisoryInfo;
import com.site.service.dining.NewsAdvisoryService;
import com.site.util.PropertyUtils;
import com.site.util.base.service.BaseService;
import com.site.util.exception.ApplicationException;
import com.site.util.exception.SystemException;
import com.site.util.extremetable.QueryPage;

@Service
public class NewsAdvisoryServiceImpl implements NewsAdvisoryService {
	private NewsAdvisoryDao dao;
	private NewsSortDao sortdao;

	public NewsAdvisoryDao getDao() {
		return dao;
	}

	@Resource
	public void setDao(NewsAdvisoryDao dao) {
		this.dao = dao;
	}

	public NewsSortDao getSortdao() {
		return sortdao;
	}

	@Resource
	public void setSortdao(NewsSortDao sortdao) {
		this.sortdao = sortdao;
	}

	@Override
	public Object execute(Object obj, String command) throws SystemException,
			ApplicationException {
		// 增加
		if (BaseService.COMMAND_ADD.equals(command)) {
			add((NewsAdvisoryBizBean) obj);
		} else if (BaseService.COMMAND_UPDATE.equals(command)) {
			update((NewsAdvisoryBizBean) obj);
		} else if (BaseService.COMMAND_DELETE.equals(command)) {
			delete((NewsAdvisoryBizBean) obj);
		}
		return null;
	}

	private void delete(NewsAdvisoryBizBean obj) {
		dao.delete(NewsAdvisoryInfo.class, obj.getId());
	}

	private void update(NewsAdvisoryBizBean obj) {
		NewsAdvisoryInfo info = new NewsAdvisoryInfo();
		PropertyUtils.copyProperties(info, obj);
		dao.update(info);
	}

	private void add(NewsAdvisoryBizBean obj) {
		NewsAdvisoryInfo info = new NewsAdvisoryInfo();
		PropertyUtils.copyProperties(info, obj);
		dao.save(info);
	}

	@Override
	public List<NewsAdvisoryBizBean> getNewsAdvisories(QueryPage queryPage,
			NewsAdvisoryBizBean newsAdvisoryBizBean) {
		List<NewsAdvisoryInfo> newsAdvisoryInfos = dao.getNewsAdvisories(
				queryPage, newsAdvisoryBizBean);
		if (null != newsAdvisoryInfos && !newsAdvisoryInfos.isEmpty()) {
			List<NewsAdvisoryBizBean> newsAdvisoryBizBeans = new ArrayList<NewsAdvisoryBizBean>();
			for (NewsAdvisoryInfo item : newsAdvisoryInfos) {
				NewsAdvisoryBizBean bean = new NewsAdvisoryBizBean();
				PropertyUtils.copyProperties(bean, item);
				newsAdvisoryBizBeans.add(bean);
			}
			if (null != newsAdvisoryBizBeans && !newsAdvisoryBizBeans.isEmpty()) {
				return newsAdvisoryBizBeans;
			}
		}
		return null;
	}

	@Override
	public NewsAdvisoryBizBean getEntityById(Long id) {
		NewsAdvisoryBizBean newsAdvisoryBizBean = new NewsAdvisoryBizBean();
		NewsAdvisoryInfo info = dao.getEntityById(NewsAdvisoryInfo.class, id);
		PropertyUtils.copyProperties(newsAdvisoryBizBean, info);
		return newsAdvisoryBizBean;
	}
}
